#include "widget.h"
#include "ui_widget.h"
#include "mytcpsocket.h"
#include "cryptstring.h"
#include "registerdialog.h"
#include "tools.h"
#include <QPushButton>
#include <QLineEdit>
#include <QTcpSocket>
#include <QThread>
#include <QLabel>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFont>
#include <QMessageBox>
Widget::Widget(QWidget *parent)
    : QWidget(parent),
      ui(new Ui::Widget),
      lineEdit_username(new QLineEdit()),
      lineEdit_passsword(new QLineEdit())
{
    ui->setupUi(this);
    init();


}

Widget::~Widget()
{
    delete ui;
}

void Widget::login()
{
    QString username = lineEdit_username->text();
    QString password = lineEdit_passsword->text();
    if(username.isEmpty()||password.isEmpty()){
        QMessageBox::question(this, "错误"
                              , "账号或密码为空！"
                              , QMessageBox::Ok|QMessageBox::Cancel
                              , QMessageBox::Cancel);
        return ;
    }
    //调用套接字成员函数，连接服务器

    clientTcpSocket->sendTLV(TASK_LOGIN_SECTION1,username.size(),username.toUtf8().data());
}

void Widget::handelLoginPacket(const Packet &packet)
{
    qDebug()<<"登录界面处理接受数据";
    int type = packet._type;
    QString info;
    switch (type){
    case TASK_LOGIN_SECTION1_RESP_OK://用户存在，则发送密文
    {
        QByteArray train = getTrainMessage(lineEdit_username->text(),packet._msg);//创建一个小火车，int + 用户名 + int + 加密密文
        clientTcpSocket->sendTLV(TASK_LOGIN_SECTION2,train.length(),train.data());//发送加密密文
        return;
    }
    case TASK_LOGIN_SECTION1_RESP_ERROR:
        info = "用户不存在或账号错误";
        break;
    case TASK_LOGIN_SECTION2_RESP_OK:
        info ="登录成功";
        break;
    case TASK_LOGIN_SECTION2_RESP_ERROR:
        info = "密码 错误";
        break;
    default://如果不属于以上消息，说明不是该窗口需要接受的消息
        return;
    }
    QMessageBox::question(this, "提示"
                          , info
                          , QMessageBox::Ok|QMessageBox::Cancel
                          , QMessageBox::Cancel);

}

void Widget::init()
{
    this->setWindowTitle("智慧监控系统登录页面");
    setMaximumSize(400,300);
    setMinimumSize(400,300);

    button_longin = new QPushButton();
    button_longin->setMinimumSize(80,40);
    button_longin->setMaximumWidth(100);
    button_longin->setText("登录");

    button_register = new QPushButton();
    button_register->setMinimumSize(80,40);
    button_register->setMaximumWidth(100);
    button_register->setText("注册");

    QFont font;
    font.setFamily("等线");

    font.setPointSize(15);
    // lineEdit_username = new QLineEdit();
    lineEdit_username->setMinimumHeight(30);
    lineEdit_username->setMaximumWidth(300);
    lineEdit_username->setAlignment(Qt::AlignHCenter);
    lineEdit_username->setFont(font);
    lineEdit_username->setMaxLength(20);

    //lineEdit_passsword = new QLineEdit();
    lineEdit_passsword->setMinimumHeight(30);
    lineEdit_passsword->setMaximumWidth(300);
    lineEdit_passsword->setAlignment(Qt::AlignHCenter);
    lineEdit_passsword->setFont(font);

    QLabel * lab_username =new QLabel();
    lab_username->setText("账号:");

    QLabel * lab_passsword =new QLabel();
    lab_passsword->setText("密码:");

    QHBoxLayout *hlayout1 = new QHBoxLayout();
    QHBoxLayout *hlayout2 = new QHBoxLayout();
    QHBoxLayout *hlayout3 = new QHBoxLayout();

    QVBoxLayout *vlayout = new QVBoxLayout(this);

    hlayout1->addStretch(1);
    hlayout1->addWidget(lab_username);
    hlayout1->addWidget(lineEdit_username);
    hlayout1->addStretch(1);

    hlayout2->addStretch(1);
    hlayout2->addWidget(lab_passsword);
    hlayout2->addWidget(lineEdit_passsword);
    hlayout2->addStretch(1);

    hlayout3->addWidget(button_longin);
    hlayout3->addWidget(button_register);

    vlayout->addLayout(hlayout1);
    vlayout->addLayout(hlayout2);
    vlayout->addLayout(hlayout3);

    clientTcpSocket = new MyTcpSocket(this);
    QString ip("192.168.88.130");
    quint16 port(8000);
    clientTcpSocket->connectToHost(ip, port);


    connect(button_longin, &QPushButton::clicked,this,&Widget::login);

    connect(clientTcpSocket, &MyTcpSocket::connected, [=](){
        qDebug() << "连接成功！";
        QMessageBox::information(this, "提示", "连接成功！");
    });

    connect(clientTcpSocket, &MyTcpSocket::disconnected, [=](){
        qDebug() << "连接断开！";
        QMessageBox::information(this, "提示", "连接断开！");
    });

    connect(clientTcpSocket, &MyTcpSocket::readPacketFinished,this,&Widget::handelLoginPacket);//将packet接受完成信号和处理函数绑定

    // connect(clientTcpSocket, &MyTcpSocket::readPacketFinished,m_register,&RegisterDialog::handelSignupPacket);
    connect(button_register, &QPushButton::clicked,[=](){
        m_register =new  RegisterDialog(clientTcpSocket,this);
        connect(clientTcpSocket, &MyTcpSocket::readPacketFinished,m_register,&RegisterDialog::handelSignupPacket);
        m_register->exec();//模态对话框
    });
}

